package com.linc.lambdastudy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import junit.framework.Assert;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.Character.isDigit;
import static java.util.Arrays.asList;
import static junit.framework.Assert.assertEquals;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        learnStream();
    }

    private void learnStream() {
        //1. collet(toList()) usage
        List<String> collected = Stream.of("a","b","c")
                .collect(Collectors.toList());
        assertEquals(asList("a","b","c"),collected);


        //2.map 操作
        collected = Stream.of("a","b","hello")
                .map(string -> string.toUpperCase())
                .collect(Collectors.toList());
        assertEquals(asList("A","B","HELLO"),collected);

        //3.filter
        List<String> beginningWithNumbers = Stream.of("a","1abc","abc1")
                .filter(value->isDigit(value.charAt(0)))
                .collect(Collectors.toList());
        assertEquals(asList("1abc"),beginningWithNumbers);

        //4.flatMap
        //flatmap 方法可用Stream替换值，然后将多个Stream连接成一个Sream
        List<Integer> together = Stream.of(asList(1,2),asList(3,4))
                .flatMap(numbers->numbers.stream())
                .collect(Collectors.toList());
        assertEquals(asList(1,2,3,4),together);

        //5.max and min
        //Comparator comparing static 方法
        //新的语言特性：默认方法
        //接口无成员变量，默认方法只能通过调用子类的方法来修改子类本身，避免了对子类的实现做各种假设。

        //6.Optional对象

        //7.方法引用

        //RxJava 响应式编程
        //Stream是为了计算最终结果，RxJava在线程模型上更像CompletableF
    }
}
